import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/login'
import Layout from '@/components/layout/index'
import Home from "@/views/home/index";
import Error from "@/views/error-page/404"
import StudentInfo from "@/views/studentInfo/studentInfo"
import Setting from "@/views/settings"
import Echarts from "@/views/echarts/echarts"
import Ech from "@/views/echarts/index"
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/echarts',
    name: 'echarts',
    component: Echarts
  },
  {
    path: '/ech',
    name: 'ech',
    component: Ech
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        name: 'home',
        component: Home
      },
      {
        path: '/studentInfo',
        name: 'studentInfo',
        component: StudentInfo
      },
      {
        path: '/clazz',
        component: Layout,
        redirect: '/class/table2',
        name: 'Example',
        children: [
          {
            path: 'list',
            name: 'Table',
            component: () => import('@/views/clazz/list'),
          },
          {
            path: 'add',
            name: 'Tree',
            component: () => import('@/views/clazz/add'),
          }
        ]
      },
      {
        path: '/teacher',
        component: Layout,
        redirect: '/teacher/manage',
        name: 'Example',
        children: [
          {
            path: 'list',
            name: 'Table',
            component: () => import('@/views/teacher/list'),
            meta: { title: '查看教师信息', icon: 'table' }
          },
          {
            path: 'add',
            name: 'Tree',
            component: () => import('@/views/teacher/add'),
          },
          {
            path: 'search',
            name: 'Tree',
            component: () => import('@/views/teacher/search'),
          }
        ]
      },
      {
        path: '/settings',
        name: 'settings',
        component: Setting
      },
    ]
  },
  {
    path: '/',
    name: 'Error',
    component: Error
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const user = JSON.parse(window.localStorage.getItem('user'))
  if (to.path !== '/login') {
    if (user) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
